/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.DAO;

import DBConnect.DBConnect;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 
 */
public class AdminDAO implements IAdmin{

    @Override
    public ArrayList<Admin> getAll() {
      //  throw new UnsupportedOperationException("Not supported yet.");
         ArrayList<Admin> list = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        if(DBConnect.open()){
            try {
                ps= DBConnect.cnn.prepareStatement("select * from user_ad");
                rs = ps.executeQuery();
                list = new ArrayList<Admin>();
                
                while (rs.next()) {
                    
                 list.add(new Admin(rs.getString("id"), rs.getString("username"),rs.getString("password")));
                    
                }
            } catch (SQLException ex) {
                Logger.getLogger(AdminDAO.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
            
                DBConnect.close(ps, rs);
            }
            
        
        }else{
        
            //phai mo ra 1 form cho nhap user nam passs, url , driver
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }
        
        
        return  list;
    }

    @Override
    public Admin addNew(Admin ad) {
      //  throw new UnsupportedOperationException("Not supported yet.");
         PreparedStatement ps = null;
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("insert into user_ad (username,password) values (?,?)");
                ps.setString(1, ad.getName());
                ps.setString(2, ad.getPass());
                if (ps.executeUpdate() > 0) {
                  System.out.print("ok");  //ok
                } else {
                    ad = null;
                }
      
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi ");
            } finally {
                 DBConnect.close(ps);
            }
        
         } else {
            ad = null;
        }
        return ad;
    }

    @Override
    public boolean update(String id) {
        throw new UnsupportedOperationException("Not supported yet.");
       
        
    }

    @Override
    public boolean delete(String id) {
       // throw new UnsupportedOperationException("Not supported yet.");
         PreparedStatement ps = null; 
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("delete from user_ad where id = ?");
                ps.setString(1,id);
                int rs = ps.executeUpdate();
       
             if(rs>0) {
                 return true;
             }
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi ");
            } finally {
                 DBConnect.close(ps);
            }
        
        }else{
        
            //phai mo ra 1 form cho nhap user nam passs, url , driver
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }
        
        
        return false;
    }
    }
    

